package zifuchuan

func StrStr(haystack string, needle string) int {
	a, b := len(haystack), len(needle)
	if a < b {
		return -1
	}
	isTrue := -1
	first := true
	x, y := []byte(haystack), []byte(needle)
	for i := 0; i < a; i++ {
		if y[0] == x[i] && first {
			for j := i; j < b+i; j++ {
				if y[j-i] == x[j] && j-i == b-1 {
					isTrue = i
					first = false
				}
			}

		}
	}
	return isTrue
}

func strStr(haystack string, needle string) int {
	a, b := len(haystack), len(needle)
	if b == 0 {
		return 0 // 按照惯例，空字符串始终匹配开头
	}
	if a < b {
		return -1
	}

	x := []byte(haystack)
	y := []byte(needle)

	for i := 0; i <= a-b; i++ {
		match := true
		for j := 0; j < b; j++ {
			if x[i+j] != y[j] {
				match = false
				break
			}
		}
		if match {
			return i
		}
	}
	return -1
}
